package thread.unit1.exetend;


/**
 * 让三个线程有顺序的交替进行
 *  每执行一个，都会停顿一秒再进行下一个
 *
 */
public class ThreadW2 {
    static int status  = 1;
//    static Object object = new Object();

    public synchronized void runA(){
            while (status!=1) {

                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"--执行");
                status = 2;
                this.notifyAll();
    }
    public synchronized void runB(){
            while (status!=2) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"--执行");
                status = 3;
                this.notifyAll();
    }
    public synchronized void runC(){

            while (status!=3) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
                System.out.println(Thread.currentThread().getName()+"--执行");
                status = 1;
                this.notifyAll();
    }

    public static void main(String[] args) {
        ThreadW2 w2 = new ThreadW2();

        Thread A = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    w2.runA();

                }
//                synchronized (object) {
//                    while (status == 1) {
////                    w2.runB();
//                        System.out.println(Thread.currentThread().getName() + "--执行");
//                        try {
//                            object.wait();
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                        status = 2;
//                        this.notifyAll();
//                    }
//                }

            }
        },"线程A");

        Thread B = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    w2.runB();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
//                synchronized (object) {
//                    while (status == 2) {
////                    w2.runB();
//                        System.out.println(Thread.currentThread().getName() + "--执行");
//                        try {
//                            object.wait();
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//
//                        status = 3;
//                        this.notifyAll();
//                    }
//                }
            }
        },"线程B");

        Thread C = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    w2.runC();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
//                synchronized (object) {
//                    while (status == 3) {
////                    w2.runB();
//                        System.out.println(Thread.currentThread().getName() + "--执行");
//                        try {
//                            object.wait();
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                        status = 1;
//                        this.notifyAll();
//                    }
//                }
            }
        },"线程C");

        A.start();
        B.start();
        C.start();
    }
}

